<template>
  <div class="login">
    <Logo />
    <van-form @submit="onSubmit">
      <van-field
        class="line"
        v-model="userName"
        name="userName"
        label="用户名"
        placeholder="请输入用户名"
        :rules="[{ required: true, message: '请填写用户名' }]"
      />
      <van-field
        class="line"
        v-model="password"
        type="password"
        name="password"
        label="密码"
        placeholder="请输入密码"
        :rules="[{ required: true, message: '请填写密码' }]"
      />
      <div style="margin: 16px">
        <van-button
          round
          block
          type="info"
          native-type="submit"
          style="margin-top: 2rem"
          >登录</van-button
        >
      </div>
      <div class="changePage">
        <router-link :to="{ name: 'Reg' }">没有账号,我要注册</router-link>
      </div>
    </van-form>
  </div>
</template>

<script>
import { Toast } from "vant";
import Logo from "../components/Logo.vue";
import { loginAPI } from "../services/auth";
import { setToken } from "../utils/tools";

export default {
  data() {
    return {
      userName: "",
      password: "",
    };
  },
  methods: {
    async onSubmit(values) {
      // console.log("submit", values);
      const res = await loginAPI(values);
      // console.log(res);
      if (res.code) {
        Toast.success("登录成功");
        // token 存本地
        setToken(res.data);
        // 跳转我的
        this.$router.push({
          name: "User",
        });
      } else {
        Toast.fail(res.data);
      }
    },
  },
  components: {
    Logo,
  },
};
</script>

<style scoped>
.login {
  background-color: #fff;
  height: 100%;
}
.changePage {
  text-align: center;
  font-size: 0.9rem;
  margin-top: 2rem;
}
.line {
  height: 4rem;
  line-height: 4rem;
  font-size: 1rem;
}
</style>